package com.mycompany.myapp.service;

import com.mycompany.myapp.domain.ElectricBicycle;
import com.mycompany.myapp.domain.Stewarder;
import com.mycompany.myapp.repository.ElectricBicycleRepository;
import com.mycompany.myapp.repository.StewarderRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Create by wys on 2018/7/23
 */

@Service
@Transactional
public class ElectricBicycleService implements ElectricBicycleServiceInterface {

    @Autowired
    ElectricBicycleRepository electricBicycleRepository;

    @Autowired
    StewarderRepository stewarderRepository;

    private final Logger log = LoggerFactory.getLogger(ElectricBicycleService.class);

    @Override
    public List<ElectricBicycle> getAllElectricBicycleUsed() {
        List<ElectricBicycle> electricBicycleList = electricBicycleRepository
            .findElectricBicyclesByStewardStateAndUseState("false","true");
        return electricBicycleList;
    }

    @Override
    public ElectricBicycle getOneElectricBicycleUsed() {
        List<ElectricBicycle> electricBicycles = electricBicycleRepository
        .findElectricBicyclesByStewardStateAndUseState("false","true");

        ElectricBicycle electricBicycle = electricBicycles.get(0);

        return electricBicycle;
    }

    @Override
    public List<ElectricBicycle> getMaintenanceBicycle() {
        List<ElectricBicycle> electricBicycleList = electricBicycleRepository
            .findElectricBicyclesByStewardStateAndUseState("true","true");
        return electricBicycleList;
    }

    /**
     * 指定一辆需要维护的电动车发送给空闲的维护员
     * 成功:电动车需要维护，维护员空闲
     * state
     * electricBicycle_null_steward_null   空
     * success  成功
     * electricBicycle_true_steward_false  车坏维护员不闲
     * electricBicycle_false_steward_true  车不需要维护
     * electricBicycle_false_steward_false
     * unknown 未知
     *
     * @param bicycleID
     * @param stewardID
     * @return 车的维护对接 状态
     */
    @Override
    public String maintenanceBicycleToSteward(String bicycleID, String stewardID) {

        String state = "";

        ElectricBicycle electricBicycle = electricBicycleRepository
            .findElectricBicycleByElectricBicycleID(bicycleID);

        Stewarder stewarder = stewarderRepository
            .findStewardByStewarderID(stewardID);

        /**
         * test log
         * log.debug("---------------------elect: "+electricBicycle+" ----- " +
         "-------------steward: "+steward+"----binfo: "+
         electricBicycle.getBicycleInfo().equals("true") + "----swerad:---" +steward.getStewardID().equals("false"));

         */

        log.debug("------------electricBicycle:"+electricBicycle+"   "+"-----------stewarder:"+stewarder);

        if(electricBicycle == null || stewarder == null) {
            state = "electricBicycle_null_and_steward_null";
        }else if (electricBicycle.getStewardState().equals("true") && electricBicycle.getUseState().equals("true") && stewarder.getStewardState().equals("true")) {
            /**
             * test log
             log.debug("----------maintenanceBicycleToSteward success");
             */
            state = "success";
            /**
             * 匹配成功 更新state数据
             */
            electricBicycle.setStewardState("false");
            stewarder.setStewardState("false");
            stewarder.setStewardBicycle(electricBicycle.getElectricBicycleID());

            electricBicycleRepository.save(electricBicycle);
            stewarderRepository.save(stewarder);

        }else if(electricBicycle.getStewardState().equals("true") && electricBicycle.getUseState().equals("true") && stewarder.getStewardState().equals("false")) {
            state = "electricBicycle_true_steward_false";
        }else if(stewarder.getStewardBicycle().toString().equals(electricBicycle.getElectricBicycleID().toString())) {
            state = electricBicycle.getElectricBicycleID()+"电动车已经在维护   already_steward";
        }else if(electricBicycle.getStewardState().equals("false")) {
            state = "electricBicycle_false";
        }else if (stewarder.getStewardState().equals("false")){
            state = "steward_false";
        }else {
            state = "unknown";
        }

        log.debug("--------------"+state);
        return state;
    }
}
